package com.cf.mvp.rx;

import io.reactivex.Observable;

import static androidx.core.util.Preconditions.checkNotNull;

/**
 * @author yangji
 */
public class RxLifecycle {

    public static <T, R> LifecycleTransformer<T> bindEvent(Observable<R> lifecycle, R event) {
        checkNotNull(lifecycle, "lifecycle == null");
        checkNotNull(event, "event == null");
        return bind(takeUntil(lifecycle, event));
    }

    private static <R> Observable<R> takeUntil(Observable<R> lifecycle, R event) {
        return lifecycle.filter(lifecycleEvent -> lifecycleEvent.equals(event));
    }

    private static <T, R> LifecycleTransformer<T> bind(Observable<R> lifecycle) {
        return new LifecycleTransformer<>(lifecycle);
    }
}
